﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Web.Common;
using StoragePeriod.Common;
using StoragePeriod.Model.StorageForecast;
using StoragePeriod.BLL.StorageForecast;

namespace StoragePeriod.Web.SysManage
{
    public partial class QuotaTypeEdit : SecurityPage
    {
        T_RawMaterialType tRawMaterialType = new T_RawMaterialType();//类别操作

        public override string PrivCode()
        {
            return "ad8cc3dbb7146df5";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["EditType"] != null && Request.QueryString["EditType"].ToString() == "Edit")
                {
                    this.LoadData();
                }
            }

        }

        //加载数据
        private void LoadData()
        {
            int RawTypeId = Convert.ToInt32(Request.QueryString["TypeId"].ToString());
            E_RawMaterialType eRawMaterialType = tRawMaterialType.GetModel(RawTypeId);
            string strTypeName = eRawMaterialType.TypeName;
            string RawTypeName = eRawMaterialType.RawTypePath;
            foreach (Control item in this.Pan.Controls)
            {
                CheckBox cb = item as CheckBox;
                if (cb != null)
                {
                    int TypeID = int.Parse(cb.ID.ToString().Replace("rawType_", ""));
                    cb.Checked = (RawTypeName.IndexOf("|" + TypeID + "|") > -1);
                }
            }
            this.txt_RawTypeName.Text = strTypeName;
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int Pid = int.Parse(Request.QueryString["Pid"].ToString());
            if (Request.QueryString["EditType"].ToString() == "Add")
            {
                E_RawMaterialType eRawMaterialType = new E_RawMaterialType();
                eRawMaterialType.ParentId = Pid;
                eRawMaterialType.TypeName = this.txt_RawTypeName.Text.Trim();
                eRawMaterialType.TypeInfoid = 5; //指标详细
                string RawTypeName = "";
                foreach (Control item in this.Pan.Controls)
                {
                    CheckBox cb = item as CheckBox;
                    if (cb != null && cb.Checked)
                    {
                        int TypeID = int.Parse(cb.ID.ToString().Replace("rawType_", ""));
                        RawTypeName += "|" + TypeID;
                    }
                }
                if (RawTypeName.Length > 0)
                {
                    RawTypeName = RawTypeName + "|";
                }
                eRawMaterialType.RawTypePath = RawTypeName;
                tRawMaterialType.Add(eRawMaterialType);
            }
            else
            {
                int RawTypeId = Convert.ToInt32(Request.QueryString["TypeId"].ToString());
                E_RawMaterialType md = tRawMaterialType.GetModel(RawTypeId);
                md.TypeName = this.txt_RawTypeName.Text.Trim();
                string RawTypeName = "";
                foreach (Control item in this.Pan.Controls)
                {
                    CheckBox cb = item as CheckBox;
                    if (cb != null && cb.Checked)
                    {
                        int RawTypeID = int.Parse(cb.ID.ToString().Replace("rawType_", ""));
                        RawTypeName += "|" + RawTypeID;
                    }
                }
                if (RawTypeName.Length > 0)
                {
                    RawTypeName = RawTypeName + "|";
                }
                md.RawTypePath = RawTypeName;
                tRawMaterialType.Update(md);
            }

            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('保存成功！');parent.window.location.href='/SysManage/RawTypeInfoManage.aspx?Pid=" + Pid + "&TypeId=5';</script>");
        }
    }
}